#include "customslider.h"
#include <QPalette>
#include <QLabel>
#include <QMouseEvent>

#define LABEL_HEIGHT   5

CustomSlider::CustomSlider(QWidget *parent):QSlider(parent)
{
   m_displayLabel=new QLabel(this);
   m_displayLabel->setFixedSize(QSize(20,20));
   //设置游标背景为白色
   //m_displayLabel->setAutoFillBackground(true);
//   QPalette palette;
//   palette.setColor(QPalette::Background, Qt::white);
//   m_displayLabel->setPalette(palette);

   m_displayLabel->setAlignment(Qt::AlignCenter);

   m_displayLabel->setVisible(true);
   m_displayLabel->move(0,LABEL_HEIGHT);
}

CustomSlider::~CustomSlider()
{

}

void CustomSlider::mousePressEvent(QMouseEvent *event)
{
    if(!m_displayLabel->isVisible())
    {
        m_displayLabel->setVisible(true);
        m_displayLabel->setText(QString::number(this->value()));
    }
    QSlider::mousePressEvent(event);
}

void CustomSlider::mouseReleaseEvent(QMouseEvent *event)
{
    if(m_displayLabel->isVisible())
    {
        m_displayLabel->setVisible(true);
    }
    QSlider::mouseReleaseEvent(event);
}

void CustomSlider::mouseMoveEvent(QMouseEvent *event)
{
    m_displayLabel->setText(QString::number(this->value()));
    //m_displayLabel->move((this->width()-m_displayLabel->width())*this->value()/(this->maximum()-this->minimum()),LABEL_HEIGHT);
    QSlider::mouseMoveEvent(event);
}
